package com.apache.tools;

import java.util.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * description: xml生成工具
 *
 * @author dyh 创建时间：2018年07月28日
 */
public class Element {

	private String name;
	private String nodeText = "";

	private Map<String, String> property = new HashMap();

	private boolean isleaf = true;

	private List<Element> child = new ArrayList();

	private static String lt = "<";
	private static String ltEnd = "</";
	private static String rt = ">";
	private static String quotes = "\"";
	private static String equal = "=";
	private static String blank = " ";

	public Element(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNodeText() {
		return this.nodeText;
	}

	public void setNodeText(String nodeText) {
		this.nodeText = nodeText;
	}

	public Map<String, String> getProperty() {
		return this.property;
	}

	public void setProperty(Map<String, String> property) {
		this.property = property;
	}

	public boolean isIsleaf() {
		return this.isleaf;
	}

	public void setIsleaf(boolean isleaf) {
		this.isleaf = isleaf;
	}

	public List<Element> getChild() {
		return this.child;
	}

	public void setChild(List<Element> child) {
		this.child = child;
		if ((this.isleaf) && (this.child.size() > 0))
			this.isleaf = false;
	}

	public void addProperty(String key, String value) {
		this.property.put(key, value);
	}

	public void addChild(Element el) {
		this.child.add(el);
		if ((this.isleaf) && (this.child.size() > 0))
			this.isleaf = false;
	}

	public void addChild(JSONObject json) {
		if (null == json)
			return;
		Set<String> set = json.keySet();
		for (String key : set) {
			Element element = new Element(key);
			if (json.get(key) instanceof JSONObject) {
				addChilds(element, json.getJSONObject(key));
			} else if (json.get(key) instanceof JSONArray) {
				element = new Element("list");
				element.addProperty("name", key);
				JSONArray arrays = json.getJSONArray(key);
				for (int i = 0; i < arrays.size(); i++) {
					Element childs = new Element(key);
					JSONObject job = arrays.getJSONObject(i);
					addChilds(childs, job);
					element.addChild(childs);
				}
			} else {
				element.setNodeText(json.getString(key));
			}
			addChild(element);
		}
	}

	private void addChilds(Element element, JSONObject json) {
		Set<String> set = json.keySet();
		for (String key : set) {
			Element suEl = new Element(key);
			if (json.get(key) instanceof JSONObject) {
				addChilds(suEl, json.getJSONObject(key));
			} else if (json.get(key) instanceof JSONArray) {
				suEl = new Element("list");
				suEl.addProperty("name", key);
				JSONArray arrays = json.getJSONArray(key);
				for (int i = 0; i < arrays.size(); i++) {
					Element childs = new Element(key);
					JSONObject job = arrays.getJSONObject(i);
					addChilds(childs, job);
					suEl.addChild(childs);
				}
			} else {
				suEl.setNodeText(json.getString(key));
			}
			element.addChild(suEl);
		}
	}

	/**
	 * 转成xml字符串
	 * @param el
	 * @return
	 */
	public static String toxml(Element el) {
		String xmlPrefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		return xmlPrefix + elementToXml(el);
	}

	public static String elementToXml(Element el) {
		StringBuilder result = new StringBuilder();
		result.append(lt).append(el.getName());
		Iterator iter;
		if ((el.getProperty() != null) && (el.getProperty().size() > 0)) {
			iter = el.getProperty().keySet().iterator();
			while (iter.hasNext()) {
				String key = String.valueOf(iter.next());
				String value = (String) el.getProperty().get(key);
				result.append(blank).append(key).append(equal).append(quotes).append(value).append(quotes)
						.append(blank);
			}
		}
		result.append(rt);
		if (el.isIsleaf())
			result.append(el.getNodeText());
		else {
			for (Element element : el.getChild()) {
				result.append(elementToXml(element));
			}
		}
		result.append(ltEnd).append(el.getName()).append(rt);
		return result.toString();
	}

	public static void main(String args[]) {
		String jsonStr = "{\n" + "\t\"requestCode\":\"OFFICIAL_WEBSITE\",\n" + "\t\"dealType\":\"policy_list_query\",\n"
				+ "\t\"insruance_category\":\"1\",\n" + "\t\"content\":{\n"
				+ "\t\t\"policyNo\":\"10200000103000120181977356\",\n"
				+ "\t\t\"customerCellphoneNo\":\"17301287781\",\n" + "\t\t\"certificateNo\":\"412725199007164249\",\n"
				+ "\t\t\"customerName\":\"孙婷婷\",\n" + "\t\t\"pageNo\":1,\n" + "\t\t\"pageAcount\":5,\n"
				+ "\t\t\"testList\":[\n" + "\t\t\t{\n" + "\t\t\t\t\"test1\":\"ddee\",\n"
				+ "\t\t\t\t\"test2\":\"mmmd\"\n" + "\t\t\t\t\n" + "\t\t\t},\n" + "\t\t\t{\n"
				+ "\t\t\t\t\"test1\":\"ddee\",\n" + "\t\t\t\t\"test2\":\"mmmd\"\n" + "\t\t\t\t\n" + "\t\t\t}\n"
				+ "\t\t]\n" + "\t},\n" + "\t\"timestamp\":1533893621\n" + "}";
		JSONObject json = JSONObject.fromObject(jsonStr);
		Element el = new Element("root");
		el.addChild(json);
		System.out.println(Element.toxml(el));
	}
}
